//
//  PHAboutViewController.m
//  PhnixHome
//
//  Created by Levy Xu on 2018/1/6.
//  Copyright © 2018年 Levy Xu. All rights reserved.
//

#import "PHAboutViewController.h"

@interface PHAboutViewController ()

@end

@implementation PHAboutViewController

-(void)initTableView {
    [super initTableView];
    
    UIView *header = [[UIView alloc] initWithFrame:CGRectMake(0, 0, APP_WIDTH, 120)];
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(APP_WIDTH / 2 - 80, 30, 160, 60)];
    imageView.image = UIImageMake(@"phnix_red");
    [header addSubview:imageView];
    
    NSDictionary *infoDic = [[NSBundle mainBundle] infoDictionary];
    
    NSString *appVersion = [infoDic objectForKey:@"CFBundleShortVersionString"];
    NSString *appName = [infoDic objectForKey:@"CFBundleDisplayName"];
    
    QMUILabel *label1 = [[QMUILabel alloc] initWithFrame:CGRectMake(20, 90, APP_WIDTH - 40, 20)];
    label1.textAlignment = NSTextAlignmentCenter;
    label1.textColor = UIColorGray4;
    label1.font = CodeFontMake(18);
    label1.text = [NSString stringWithFormat:@"%@ %@",appName, appVersion];
    [header addSubview:label1];
    self.tableView.tableHeaderView = header;
    
    QMUIStaticTableViewCellDataSource *dataSource = [[QMUIStaticTableViewCellDataSource alloc] initWithCellDataSections:@[ @[
      [self cellWith:0 name:@"去评分"],
      [self cellWith:1 name:@"芬尼官网"]
      ]]];
    self.tableView.qmui_staticCellDataSource = dataSource;
    
    QMUILabel *label2 = [[QMUILabel alloc] initWithFrame:CGRectMake(20, APP_HEIGHT - 50 - IPhoneXSafeAreaInsets.bottom, APP_WIDTH - 40, 20)];
    label2.textAlignment = NSTextAlignmentCenter;
    label2.textColor = UIColorGray4;
    label2.font = CodeFontMake(14);
    label2.text = @"芬尼克兹 版权所有";
    [self.tableView addSubview:label2];
    
    QMUILabel *label3 = [[QMUILabel alloc] initWithFrame:CGRectMake(20, APP_HEIGHT - 25 - IPhoneXSafeAreaInsets.bottom, APP_WIDTH - 40, 15)];
    label3.textAlignment = NSTextAlignmentCenter;
    label3.textColor = UIColorGray4;
    label3.font = CodeFontMake(12);
    label3.text = @"Copyright © 2018 Phnix.All Rights Reserved.";
    [self.tableView addSubview:label3];
}

-(QMUIStaticTableViewCellData *)cellWith:(NSInteger)identifier name:(NSString *)name {
    QMUIStaticTableViewCellData *d = [[QMUIStaticTableViewCellData alloc] init];
    d.identifier = identifier;
    d.style = UITableViewCellStyleDefault;
    d.text = name;
    d.didSelectTarget = self;
    d.didSelectAction = @selector(handleDisclosureIndicatorCellEvent:);
    d.accessoryType = QMUIStaticTableViewCellAccessoryTypeDisclosureIndicator;
    return d;
}

- (void)handleDisclosureIndicatorCellEvent:(QMUIStaticTableViewCellData *)cellData {
    if (cellData.identifier == 0) {
        NSString *urlString = @"https://itunes.apple.com/us/app/itunes-u/id1322947067?action=write-review&mt=8";
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString]];
    }
    else if (cellData.identifier == 1) {
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://www.phnix.com.cn/"]];
    }
}

- (void)setupNavigationItems {
    [super setupNavigationItems];
    self.title = NSLocalizedString(@"settings_about", nil);
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 1.0f;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    return [UIView new];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return TableViewCellNormalHeight;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView.qmui_staticCellDataSource didSelectRowAtIndexPath:indexPath];
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}
@end
